package com.ydc.ln.commons.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Java日期处理类
 * 
 * @author ydc
 * @date 下午11:43:01 2013-3-11
 * @version V1.0
 */
public class DateUtils {

	/**
	 * 静态常量
	 */
	public static final String TIME_PATTON_DEFAULT = "yyyy-MM-dd HH:mm:ss";

	public static final String DATE_PATTON_DEFAULT = "yyyy-MM-dd";

	/**
	 * 任意格式日期字符串-->java.util.Date
	 * 
	 * @Title: str2date
	 * @param dateStr
	 * @param patton
	 * @return Date
	 * @throws
	 */
	public static Date str2date(String dateStr, String patton) {
		if (dateStr == null || "".equals(dateStr)) {
			return null;
		}
		if (patton == null || "".equals(patton)) {
			patton = TIME_PATTON_DEFAULT;
		}
		return getSimpleDateFormat(patton).parse(dateStr, new ParsePosition(0));
	}

	/**
	 * java.util.Date-->任意格式日期字符串
	 * 
	 * @Title: date2str
	 * @param date
	 * @param patton
	 * @return String
	 * @throws
	 */
	public static String date2str(Date date, String patton) {
		if (date == null) {
			return null;
		}
		if (patton == null || "".equals(patton)) {
			patton = TIME_PATTON_DEFAULT;
		}
		return getSimpleDateFormat(patton).format(date);
	}

	/**
	 * 任意格式日期字符串-->任意格式日期字符串
	 * 
	 * @Title: str2str
	 * @param dateStr
	 * @param srcPatton
	 * @param destPatton
	 * @return String
	 * @throws
	 */
	public static String str2str(String dateStr, String srcPatton, String destPatton) {
		Date date = DateUtils.str2date(dateStr, srcPatton);
		return DateUtils.date2str(date, destPatton);
	}

	/**
	 * 获取当前时间
	 * 
	 * @Title: getSysDate
	 * @return Date
	 * @throws
	 */
	public static Date getSysDate() {
		// Calendar.getInstance().getTime();
		return new Date(System.currentTimeMillis());
	}

	/**
	 * 获取指定输出格式的SimpleDateFormat
	 * 
	 * @Title: getSimpleDateFormat
	 * @param patton
	 * @return SimpleDateFormat
	 * @throws
	 */
	public static SimpleDateFormat getSimpleDateFormat(String patton) {
		SimpleDateFormat sdf;
		if (patton == null) {
			patton = TIME_PATTON_DEFAULT;
		}
		sdf = new SimpleDateFormat(patton);
		return sdf;
	}

	/**
	 * 基于日期类型的日期比较
	 * 
	 * @Title: compareDate
	 * @param date1
	 * @param date2
	 * @return int
	 * @throws
	 */
	public static int compareDate(Date date1, Date date2) {
		// 也可使用java.util.Date.after(java.util.Date)来比较
		int result = date1.compareTo(date2);
		return result;
	}

	/**
	 * 基于字符串类型的日期比较
	 * 
	 * @Title: compareDateStr
	 * @param dateStr1
	 * @param dateStr2
	 * @param patton
	 * @return int
	 * @throws
	 */
	public static int compareDateStr(String dateStr1, String dateStr2, String patton) {
		Date date1 = DateUtils.str2date(dateStr1, patton);
		Date date2 = DateUtils.str2date(dateStr2, patton);
		// dateStr1.compareTo(dateStr2);
		return compareDate(date1, date2);
	}

	/**
	 * Date-->Calendar
	 * 
	 * @Title: date2calendar
	 * @param date
	 * @return Calendar
	 * @throws
	 */
	public static Calendar date2calendar(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * Calendar-->Date
	 * 
	 * @Title: calendar2date
	 * @param calendar
	 * @return Date
	 * @throws
	 */
	public static Date calendar2date(Calendar calendar) {
		if (calendar == null) {
			return null;
		}
		return calendar.getTime();
	}

	public static void main(String[] args) {
		String patton1 = "yyyy/MM/dd hh:mm:ss";
		String patton2 = "yyyy-MM-dd hh:mm:ss";
		Date now = DateUtils.getSysDate();
		System.out.println("current date:\t\t" + now);
		System.out.print(patton1 + "\t");
		String dateStr1 = DateUtils.date2str(now, patton1);
		String dateStr2 = DateUtils.date2str(now, patton2);
		System.out.println(dateStr1);
		System.out.print(patton2 + "\t");
		System.out.println(dateStr2);

		Date date1 = DateUtils.str2date("2013-03-12 01:09:18", patton2);
		Date date2 = DateUtils.str2date("2013-03-12 01:15:38", patton2);
		int result = DateUtils.compareDate(date1, date2);
		System.out.println("result:\t" + result);
	}

}
